#!/usr/bin/python
# -*- coding:utf-8 -*-
# @FileName : Test5.py
# Author    : myh
import matplotlib.pyplot as plt
import torch
import torchvision
from torch.utils import data
from torchvision import transforms
from d2l import torch as d2l



def get_fashion_mnist_labels(labels):  # @save
    """返回Fashion-MNIST数据集的文本标签"""
    text_labels = ['t-shirt', 'trouser', 'pullover', 'dress', 'coat',
                   'sandal', 'shirt', 'sneaker', 'bag', 'ankle boot']
    return [text_labels[int(i)] for i in labels]


def show_images(imgs, num_rows, num_cols, titles=None, scale=1.5):  # @save
    """绘制图像列表"""
    figsize = (num_cols * scale, num_rows * scale)
    _, axes = d2l.plt.subplots(num_rows, num_cols, figsize=figsize)
    axes = axes.flatten()
    for i, (ax, img) in enumerate(zip(axes, imgs)):

        if torch.is_tensor(img):
            # 图片张量
            # ax.imshow(img.numpy())
            plt.imshow(img.numpy())
            plt.show()
        else:
            # PIL图片
            # ax.imshow(img)
            plt.imshow(img.numpy())
            plt.show()
        ax.axes.get_xaxis().set_visible(False)
        ax.axes.get_yaxis().set_visible(False)
        if titles:
            ax.set_title(titles[i])
    return axes


def get_dataloader_workers():  # @save
        """使用4个进程来读取数据"""
        return 4


if __name__ == '__main__':
    # d2l.use_svg_display()
    #
    # # 通过ToTensor实例将图像数据从PIL类型变换成32位浮点数格式，
    # # 并除以255使得所有像素的数值均在0～1之间
    trans = transforms.ToTensor()
    mnist_train = torchvision.datasets.FashionMNIST(
        root="../data", train=True, transform=trans, download=True)
    mnist_test = torchvision.datasets.FashionMNIST(
        root="../data", train=False, transform=trans, download=True)

    #
    # # 查看样本大小
    # print(len(mnist_train))
    # print(len(mnist_test))

    # X, y = next(iter(data.DataLoader(mnist_train, batch_size=18)))
    #
    # show_images(X.reshape(18, 28, 28), 2, 9, titles=get_fashion_mnist_labels(y))
    #
    batch_size = 256

    train_iter = data.DataLoader(mnist_train, batch_size, shuffle=True,
                                 num_workers=get_dataloader_workers())
    timer = d2l.Timer()
    for X, y in train_iter:
        continue
    print(f'{timer.stop():.2f} sec')

